package cn.bdqn.controller;

import cn.bdqn.pojo.BasDict;
import cn.bdqn.pojo.Customer;
import cn.bdqn.service.BasDictService;
import cn.bdqn.service.CustomerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    CustomerService customerService;
    @Resource
    BasDictService basDictService;

    @RequestMapping("/list")
    public String list(String custName, String custNo, String region,
                       String managerName, String level,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model){
        Integer pageSize=3;

        IPage<Customer> customersByPage = customerService.getCustomersByPage(custName, custNo, region, managerName, level, pageIndex, pageSize);
        model.addAttribute("customerPager",customersByPage);

        //查询回显
        model.addAttribute("custName",custName);
        model.addAttribute("custNo",custNo);
        model.addAttribute("region",region);
        model.addAttribute("managerName",managerName);
        model.addAttribute("level",level);

        //查询客户等级
        List<BasDict> levelList=basDictService.getDictByDictType("客户等级");
        model.addAttribute("levelList",levelList);
            //查询地区
        List<BasDict> regionList=basDictService.getDictByDictType("地区");
        model.addAttribute("regionList",regionList);
        return "customer/list";
    }
    /**
     * 跳转到修改页面
     * @param custNo
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String custNo,Model model){
        //根据客户编号，查询客户信息
        Customer customer = customerService.getCustomer(custNo);
        model.addAttribute("customer", customer);
        //查询客户等级
        List<BasDict> levelList = basDictService.getDictByDictType("客户等级");
        model.addAttribute("levelList", levelList);
        //查询地区
        List<BasDict> regionList = basDictService.getDictByDictType("地区");
        model.addAttribute("regionList", regionList);
         return "customer/edit";

    }

    /**
     * 修改
     * @param customer
     * @return
     */
    @RequestMapping("/update")
    public String update(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return "redirect:/customer/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/customer/edit?custNo="+customer.getCustNo();
        }
    }

    /**
     * 删除
     * @param custNo
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String,Object> delete(String custNo){
        Map<String,Object> map=new HashMap<>();
        try {
            customerService.deleteCustomer(custNo);
            map.put("delResult",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult",false);
        }
       return map;

    }



}
